/**
 * jims
 */
package com.jims.master.modules.csm.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.csm.dao.ExpStorageDeptDao;
import com.jims.master.modules.csm.entity.ExpStock;
import com.jims.master.modules.csm.entity.ExpStorageDept;
import com.jims.master.modules.csm.entity.ExpSupplierCata;
import com.jims.master.modules.csm.service.ExpInoutClassService;
import com.jims.master.modules.csm.service.ExpStockService;
import com.jims.master.modules.csm.service.ExpStorageDeptService;
import com.jims.master.modules.csm.service.ExpSupplierCataService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 消耗品库存管理Controller
 * @author zhuq
 * @version 2017-03-10
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expStock")
public class ExpStockController extends BaseController {
    @Autowired
    private ExpSupplierCataService expSupplierCataService;
    @Autowired
    private ExpStockService expStockService;
    @Autowired
    private ExpStorageDeptDao expStorageDeptDao;
    @Autowired
    private ExpInoutClassService expInoutClassService;
    @Autowired
    private ExpStorageDeptService expStorageDeptService;
    @ModelAttribute
    public ExpStock get(@RequestParam(required=false) String id) {
        ExpStock entity = null;
        if (StringUtils.isNotBlank(id)){
            entity = expStockService.get(id);
        }
        if (entity == null){
            entity = new ExpStock();
        }
        return entity;
    }

    /**
     * 批量入库首页
     * @param expStock
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"batchImport",""})
    public String batchImport(ExpStock expStock,HttpServletRequest request,HttpServletResponse response,Model model){
        Page<ExpStock> page = expStockService.findPage(new Page<ExpStock>(request, response), expStock);
        model.addAttribute("page",page);
        return "modules/csm/expImportBatch";
    }

    /**
     * 对消入出库首页
     * @param expStock
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"impExpIndex", ""})
    public String expImpExpBalanceIndex(ExpStock expStock, HttpServletRequest request, HttpServletResponse response, Model model) {
        String storageLevel = "";
        ExpStorageDept expStorageDept = expStorageDeptDao.get(UserUtils.getUser().getOrgDept().getId());
        if(null != expStorageDept){
            storageLevel = expStorageDept.getStorageLevel();
        }
        Page<ExpStock> page = expStockService.findPage(new Page<ExpStock>(request, response), expStock);
        model.addAttribute("page", page);
        return "modules/csm/expImpExpBalanceIndex";
    }

    /**
     * 对消入出库Form首页
     * @param expStock
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "expImpExpBalanceForm")
    public String expImpExpBalanceForm(ExpStock expStock, Model model) {
        model.addAttribute("expStock", expStock);
        return "modules/csm/expImpExpBalanceForm";
    }

    /**
     * 消耗品对消入出库保存
     * @param list
     * @param request
     * @param response
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveImpExp")
    @ResponseBody
    public StringData saveImpExp(@RequestBody List<ExpStock> list, HttpServletRequest request, HttpServletResponse response) {
        StringData data = new StringData();
        int i = expStockService.saveImpExp(list);
        if (i > 0) {
            data.setCode("success");
            data.setData("对消入出库成功");
        } else {
            data.setCode("error");
            data.setData("对消入出库失败");
        }
        return data;
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"index", ""})
    public String list(ExpStock expStock, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<String> list=expInoutClassService.findByLevel("1");
        Page<ExpStock> page = expStockService.findPage(new Page<ExpStock>(request, response), expStock);
        model.addAttribute("page", page);
        model.addAttribute("list", list);
        return "modules/csm/expStockIndex";
    }

    /**
     * 库存量查询
     * @param expStock
     * @param request
     * @param response
     * @param model
     * @param expName
     * @param expType
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"query", ""})
    public String listquery(ExpStock expStock,HttpServletRequest request, HttpServletResponse response, Model model,String expName,String expType) {
            Page<ExpStock> page = expStockService.findExpList(new Page<ExpStock>(request, response), expStock, expName, expType);
            model.addAttribute("page", page);
            return "modules/csm/expStockQuery";
        }

    @RequiresPermissions("user")
    @RequestMapping(value = {"stockSizeQuery", ""})
    public String listque(ExpStock expStock, HttpServletRequest request, HttpServletResponse response, Model model) {
        return "modules/csm/expStockQuery";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"queryBat", ""})
    public String listquerybat(ExpStock expStock, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ExpStock> page = expStockService.findExpListbat(new Page<ExpStock>(request, response), expStock);
        model.addAttribute("page", page);
        return "modules/csm/expStockQuery";
    }

    /**
     * 全院库存量查询
     * @param expStock
     * @param request
     * @param response
     * @param model
     * @param expName
     * @param expType
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"queryAll", ""})
    public String listqueryAll(ExpStock expStock, HttpServletRequest request, HttpServletResponse response, Model model,String expName,String expType) {
        Page<ExpStock> page = expStockService.findExpList(new Page<ExpStock>(request, response), expStock, expName, expType);
        model.addAttribute("page", page);
        List<ExpStorageDept> list=expStorageDeptService.findList(new ExpStorageDept());
        model.addAttribute("list",list);
        List<ExpSupplierCata> firmList=expSupplierCataService.findList(new ExpSupplierCata());
        model.addAttribute("firmList",firmList);
        return "modules/csm/expAllStockQuery";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = {"queryAllBat", ""})
    public String listqueryAllbat(ExpStock expStock, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ExpStock> page = expStockService.findExpListbat(new Page<ExpStock>(request, response), expStock);
        model.addAttribute("page", page);
        List<ExpStorageDept> list=expStorageDeptService.findList(new ExpStorageDept());
        model.addAttribute("list",list);
        List<ExpSupplierCata> firmList=expSupplierCataService.findList(new ExpSupplierCata());
        model.addAttribute("firmList",firmList);
        return "modules/csm/expAllStockQuery";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "form")
    public String form(ExpStock expStock, Model model) {
        model.addAttribute("expStock", expStock);
        return "modules/csm/expStockForm";
    }

    @RequiresPermissions("user")
    @RequestMapping(value = "save")
    @ResponseBody
    public StringData save(ExpStock expStock, Model model) {
        StringData data=new StringData();
        expStock.updateOrgId();
        int i=expStockService.save(expStock);
        if(i>0){
            data.setCode("success");
            data.setData("保存消耗品入库成功");
        }else{
            data.setCode("error");
            data.setData("保存消耗品入库失败");
        }
        return data;
    }
    @RequiresPermissions("user")
    @RequestMapping(value = "delete")
    @ResponseBody
    public StringData delete(ExpStock expStock) {
        StringData data=new StringData();
        int i=expStockService.delete(expStock);
        if(i>0){
            data.setCode("success");
            data.setData("删除消耗品入库成功");
        }else{
            data.setCode("error");
            data.setData("删除消耗品入库失败");
        }
        return data;
    }

    /*消耗品入库处理
    * @zhuq
    * 获取要入库的expList,分别插入expStock,expImportMaster,expImportDetail表
    * */
    @RequiresPermissions("user")
    @RequestMapping(value = "importExp")
    @ResponseBody
    public StringData saveImportExp(@RequestBody List<ExpStock> expStockList ,HttpServletRequest request, HttpServletResponse response) {
        StringData data=new StringData();
        String printId=expStockService.saveImportExp(expStockList);
        if(printId!="0"){
            data.setCode("success");
            data.setData(printId);
        }else{
            data.setCode("error");
        }
        return data;
    }

    /**
     * 消耗品批量入库保存
     * @param stockList
     * @param request
     * @param response
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveBatchImport")
    @ResponseBody
    public StringData saveBatchImport(@RequestBody List<ExpStock> stockList, HttpServletRequest request, HttpServletResponse response) {
        StringData data = new StringData();
        int i = expStockService.saveBatchImport(stockList);
        if(i > 0){
            data.setCode("success");
            data.setData("入库成功!");
        }else if(i == -1){
            data.setCode("error");
            data.setData("没有需要入库的数据!");
        }else{
            data.setCode("error");
            data.setData("入库失败!");
        }
        return data;
    }

    /**
     * 根据expInfoId、storageId获取当前库房下的不同批次库存大于0的list
     * @return
     * @author zhuq
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"findByInfoId", ""})
    public String saveBatchImport(ExpStock expStock, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<ExpStock> expStockList= expStockService.findByInfoId(expStock);
        model.addAttribute("expStockList", expStockList);
        return "modules/csm/expInventoryCheckForm";
    }

    /**
     * @param q
     * @param limit
     * @param storageId
     * @retun
     * @Title: autoCompleteStock
     * @Desciption: (科室申领物品, 库存数大于0的物品自动补全)
     * @author fengyg
     * @date 2017-06-16
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"autoCompleteStock", ""})
    @ResponseBody
    public List<ExpStock> autoCompleteStock(String q,String limit,String storageId){
        if (q != null && !q.equals("")) {
            try {
                q = URLDecoder.decode(q, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        List<ExpStock> list = expStockService.autoCompleteStockList(q, limit, storageId);
        return list;
    }


    /**
     * 消耗品自动补全
     * @param q 输入字符
     * @param limit 分页个数
     * @return
     * @author zhangyao
     * @date 2017-07-19
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "automatic")
    @ResponseBody
    public List<BaseDto> automatic(String q,String limit,String  storageCode) {
        String orgId= UserUtils.getOrgId();
        return  expStockService.automatic(orgId,q,limit,storageCode);
    }


}